{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "f5166243",
   "metadata": {},
   "source": [
    "## Step.1 Calculate NewtonDD table"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "882e2c85",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "def calNDDTable(xiList,yiList):\n",
    "    xiList = np.array(xiList)\n",
    "    yiList = np.array(yiList)\n",
    "    n = xiList.size\n",
    "    dimension = (n,n)\n",
    "    arrNDD = np.zeros(dimension)\n",
    "    arrNDD[:,0] = yiList\n",
    "    for j in range (1,n):\n",
    "        for i in range(j,n):\n",
    "            arrNDD[i,j] = (arrNDD[i,j-1] - arrNDD[i-1,j-1])/(xiList[i] - xiList[i-j])\n",
    "    return arrNDD"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "8b8552e6",
   "metadata": {},
   "outputs": [],
   "source": [
    "xiList = [1,2,3,4]\n",
    "yiList = [2,3,2,1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "29f02dd3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 2.        ,  0.        ,  0.        ,  0.        ],\n",
       "       [ 3.        ,  1.        ,  0.        ,  0.        ],\n",
       "       [ 2.        , -1.        , -1.        ,  0.        ],\n",
       "       [ 1.        , -1.        ,  0.        ,  0.33333333]])"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "calNDDTable(xiList,yiList)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0eddce7e",
   "metadata": {},
   "source": [
    "## Step.2 NewtonDD interpolation"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "478b2c57",
   "metadata": {},
   "outputs": [],
   "source": [
    "def interpNDD(x,xiList,yiList):\n",
    "    xiList = np.array(xiList)\n",
    "    yiList = np.array(yiList)\n",
    "    n = xiList.size\n",
    "    NDDTable = calNDDTable(xiList,yiList)\n",
    "    digNDD = np.diag(NDDTable)\n",
    "    y = 0\n",
    "    for i in range(0,n):\n",
    "        li = digNDD[i]\n",
    "        for j in range(0,i):\n",
    "            li = li * (x - xiList[j])\n",
    "        y = y + li\n",
    "    return y"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "73820d8e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2.247"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "interpNDD(1.1,xiList,yiList)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "dd499b02",
   "metadata": {},
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
